package com.dragon.modules.yuegou.service.impl.member;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.modules.yuegou.entity.member.TemplateSetEntity;
import com.dragon.modules.yuegou.enums.member.TemplateSetStatus;
import com.dragon.modules.yuegou.mapper.member.TemplateSetMapper;
import com.dragon.modules.yuegou.model.member.TemplateSet;
import com.dragon.modules.yuegou.query.member.TemplateSetQuery;
import com.dragon.modules.yuegou.service.member.TemplateSetService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 支付宝会员模板设置 服务实现
 *
 * @author pbuilder generated
 * @date 2021-10-26 20:15:21
 * @version 2.0
 */
@Service("memTemplateSetService")
public class TemplateSetServiceImpl
		extends ServiceImpl<TemplateSetMapper, TemplateSetEntity>
		implements TemplateSetService {

	@Override
	public TemplateSet saveTemplateSet(TemplateSet templateSet) {
		if(StringUtils.isNotBlank(templateSet.getId())) {
			baseMapper.updateById(templateSet);
		}else { 
			templateSet.setId(IdWorker.getIdStr());
			save(templateSet);
		}	
		return templateSet;
	}

	@Override
	public TemplateSet getTemplateSetByBusdId(String busdId, TemplateSetStatus status) {
		TemplateSetQuery query = new TemplateSetQuery();
		query.setBusdId(busdId);
		if(null != status) {
			query.setStatus(status);
		}
		return getFirst(query);
	}

	private TemplateSet getFirst(TemplateSetQuery query){
		QueryWrapper<TemplateSetEntity> wrapper = query.toQueryWrapper();
		List<TemplateSetEntity> refList = list(wrapper);
		if (CollectionUtils.isNotEmpty(refList)){
			return new TemplateSet(refList.get(0));
		}
		return new TemplateSet();
	}
	
	@Override
	public TemplateSet getTemplateByTemplateId(String templateId) {
		TemplateSetQuery query = new TemplateSetQuery();
		query.setTemplateId(templateId);
		return getFirst(query);
	}
	
	@Override
	public Long countByBizNoPrefix(String bizNoPrefix) {
		TemplateSetQuery query = new TemplateSetQuery();
		query.setBizNoPrefix(bizNoPrefix);
		return count(query.toQueryWrapper());
	}

	@Override
	public TemplateSet updTemplateSet(TemplateSet templateSet) {
		baseMapper.updateById(templateSet);
		return templateSet;
	}



}